package com.shiro;

import com.common.enums.RoleTypeEnum;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * @Description:Shiro获取安全数据
 * @date: 2018/11/23 11:27
 */
@Service
public class AuthRealm extends AuthorizingRealm
{

    @Override
    public String getName()
    {
        return "authRealm";
    }

    /**
     * 自定义token必须重写supports方法
     *
     * @param token
     * @return
     */
    @Override
    public boolean supports(AuthenticationToken token)
    {
        return token instanceof JwtToken;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        return null;
    }

    /**
     * 获取凭证
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection)
    {
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//        LoginUser loginUser = (LoginUser)principalCollection.getPrimaryPrincipal();
//        if(loginUser == null){
//            throw new AuthorizationException("授权失败");
//        }
//        simpleAuthorizationInfo.addRoles(loginUser.getRoles());
//        simpleAuthorizationInfo.addStringPermissions(loginUser.getPermissions());
        return simpleAuthorizationInfo;
    }

    /**
     * 获取凭证
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException
//    {
//        String token = (String) authenticationToken.getPrincipal();
//        if (token == null)
//        {
//            throw new AuthenticationException("token invalid");
//        }
////        String userId = JwtUtils.getAppUserId(token);
////        LoginUser loginUser = userService.getLoginUser(userId);
////        if(loginUser == null){
////            throw new SysException("获取用户信息失败");
////        }
////        loginUser.setToken(token);
//        LoginUser loginUser = new LoginUser();
//        return new SimpleAuthenticationInfo(loginUser, token, getName());
//    }

    /**
     * 管理员不拦截
     *
     * @param permission
     * @param info
     * @return
     */
    @Override
    protected boolean isPermitted(Permission permission, AuthorizationInfo info)
    {
        if (isAdminAccess(info))
        {
            return true;
        }
        return super.isPermitted(permission, info);
    }

    /**
     * 管理员不拦截
     *
     * @param roleIdentifier
     * @param info
     * @return
     */
    @Override
    protected boolean hasRole(String roleIdentifier, AuthorizationInfo info)
    {
        if (isAdminAccess(info))
        {
            return true;
        }
        return super.hasRole(roleIdentifier, info);
    }

    /**
     * 判断登录用户是否是管理员
     *
     * @param info
     * @return
     */
    private boolean isAdminAccess(AuthorizationInfo info)
    {
        if (info == null)
        {
            return false;
        }
        if (CollectionUtils.isEmpty(info.getRoles()))
        {
            return false;
        }
        if (info.getRoles().contains(RoleTypeEnum.ADMIN.getRoleType()))
        {
            return true;
        }
        return false;
    }
}
